package com.bua.control;

import android.content.Context;
import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class PaintView extends RelativeLayout {
	float preX;
	float preY;
    private OnImageClickListener clickListener;
	private Path path;
    Context mContext ;
	public Paint paint = null;
	Bitmap cacheBitmap = null;
    Bitmap BackgroundBitmap = null;
	Canvas cacheCanvas = null;
    long time;;
    public Bitmap areaBitmap = null;
	public void setStyle(int style) {
		switch (style) {
		case 1:
			paint.setStrokeWidth(18);
			paint.setColor(Color.RED);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
			
			break;
		case 2:
			paint.setStrokeWidth(6);
			paint.setColor(Color.DKGRAY);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
			
			break;
		case 3:
			paint.setStrokeWidth(24);
			paint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
			break;
		default:
			break;
		}

	}
    public void SetClickListener(OnImageClickListener l)
    {
        clickListener=l;
    }
	public PaintView(Context context ) {
		super(context);
		// TODO Auto-generated constructor stub
        mContext= context;
		this.setBackgroundColor(Color.TRANSPARENT);
		path = new Path();
		paint = new Paint(Paint.DITHER_FLAG);
		paint.setColor(Color.RED);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(6);
		paint.setAntiAlias(true);
		paint.setDither(true);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
            time = System.currentTimeMillis();
            path.moveTo(x, y);
			preX = x;
			preY = y;
			break;
		case MotionEvent.ACTION_MOVE:
			path.quadTo(preX, preY, x, y);
			preX = x;
			preY = y;
			break;
		case MotionEvent.ACTION_UP:
            if(x<areaBitmap.getWidth() && y<areaBitmap.getHeight() && ( System.currentTimeMillis()-time)<100  )
            {

                int tag =areaBitmap.getPixel((int)x,(int)y) &0x00FFFFFF;
                clickListener.onClick(tag);
            }
			cacheCanvas.drawPath(path, paint);
			path.reset();
			break;

		}
		invalidate();
		return true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint bmpPaint = new Paint();
		if (cacheBitmap == null) {
			int width = getWidth();
			int height = getHeight();
			cacheBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
			cacheCanvas = new Canvas();
			cacheCanvas.setBitmap(cacheBitmap);
		}
        if(BackgroundBitmap!=null)
        {

            cacheCanvas.drawBitmap(BackgroundBitmap, 0, 0, null);
            BackgroundBitmap.recycle();
            BackgroundBitmap=null;
        }
		canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
		canvas.drawPath(path, paint);
	}


    public void DrawImage(Bitmap bmp,int x,int y)
    {
        BackgroundBitmap=Bitmap.createBitmap(350, 600,Config.ARGB_8888);

        Canvas canvas = new Canvas(BackgroundBitmap);
        canvas.drawBitmap( bmp, x, y, null );//插入图标
        invalidate();
    }
}
